package com.jcy.jcyrxjava.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.jcy.jcyrxjava.Controller;
import com.jcy.jcyrxjava.R;
import com.jcy.jcyrxjava.module.request.ArticleWrapper;
import com.jcy.jcyrxjava.module.response.LoginResponse;
import com.jcy.jcyrxjava.net.RetrofitHelper;
import com.jcy.jcyrxjava.setDataCallBack;
import com.zhpan.idea.net.common.ResponseObserver;
import com.zhpan.idea.utils.RxUtil;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends BaseActivity implements setDataCallBack {
    setDataCallBack mCallBack;
    TextView tv_content;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        mCallBack = this;
        initView();
    }
    private void showPop(){
//       new AddressSelector()
//        show();
//        dismiss();
//        setOnSelectCompletedListener((addressSelector, province, city, area, street) ->{
//            addressSelector.dismiss;
//            city.name;//名称
//            city.code;//对应的行政编码
//        });
    }
    private void initView() {
        tv_content = findViewById(R.id.tv_content);
    }

    /**
     * Post请求
     */
    public void login(View view) {
        RetrofitHelper.getApiService()
                .login(getParameters())
                .compose(RxUtil.rxSchedulerHelper(this, true))
                .subscribe(new ResponseObserver<LoginResponse>() {
                    @Override
                    public void onSuccess(LoginResponse response) {
                        showToast("登录成功");
                    }

                    @Override
                    public void onFail(String message) {
                        super.onFail(message);

                    }
                });
    }

    private Map<String, Object> getParameters() {
        Map<String, Object> map = new HashMap<>();
        map.put("username", "110120");
        map.put("password", "123456");
        return map;
    }

    /**
     * Get请求
     *
     * @param view
     */
    public void getData(View view) {
        //这边调用控制层
        Controller mCintroller = new Controller(mCallBack);
        mCintroller.getData(this);
//        RetrofitHelper.getApiService()
//                .getArticle()
//                .compose(RxUtil.rxSchedulerHelper(this, true))
//                .subscribe(new ResponseObserver<ArticleWrapper>() {
//                    @Override
//                    public void onSuccess(ArticleWrapper response) {
//                        tv_content.setText(response.getDatas().get(0).getTitle());
//                        showToast("Request Success，size is：" + response.getDatas().size());
//                    }
//
//                    @Override
//                    public void onFail(String message) {
//                        super.onFail(message);
//                    }
//                });
    }

    @Override
    public void resultData(ArticleWrapper mArticleWrapper) {
        showToast(mArticleWrapper.getDatas().get(0).getTitle());
    }
}
